#include"RingQueue.hpp"
#include"Thread.hpp"
#include"ThreadPool.hpp"

void* Producer(void* arg)
{
    auto queue = static_cast<RingQueue<Task>*>(arg);
    while(1)
    {
        int data1 = rand() % 100;
        int data2 = rand() % 100;
        int operation = rand() % 5;
        Task task(data1, data2, opers[operation]);
        queue->push(task);
        std::cout << "Produced: " << data1 << " " << data2 << std::endl;
        sleep(1);
    }
    return nullptr;
}

void* Consumer(void* arg)
{
    auto queue = static_cast<RingQueue<Task>*>(arg);
    while(1)
    {
        // Task tmp;
        // queue->pop(tmp);
        // usleep(10);
        // tmp();
        // std::cout << "Consumed: " << tmp.GetTask()<<" "<< tmp.GetResult()<<std::endl;
    }
    return nullptr;
}


void Print()
{
    std::cout<<"Hello, World!"<<std::endl;
}

int main()
{
    // auto queue = new RingQueue<Task>(10); // 创建一个容量为10的环形队列
    // pthread_t producer[5], consumer[5];
    // for(int i=0;i<5;i++)
    // {
    //     pthread_create(producer+i, nullptr, Producer,(void*)queue);
    //     pthread_create(consumer+i, nullptr, Consumer, (void*)queue);
    // }

    // for(int i=0;i<5;i++)
    // {
    //     pthread_join(*(producer+i), nullptr);
    //     pthread_join(*(consumer+i), nullptr);
    // }
    srand((unsigned)time(nullptr));
    ThreadPool<Task>* tp=ThreadPool<Task>::GetInstance(5);
    tp->Start();
    sleep(2);
    while(1)
    {
        int data1 = rand() % 100;
        int data2 = rand() % 100;
        int operation = rand() % 5;
        auto task=new Task(data1, data2, opers[operation]);
        (*task)();
        std::cout  << task->GetTask() << " " << task->GetResult() << std::endl;
        tp->Push(*task);
        sleep(1);
    }
    // std::vector<Thread<Task>> threads;
    // for(int i=0;i<11;i++)
    // {
    //     threads.push_back(Thread<Task>(Print));
    // }
    // for(auto&e: threads)
    // {
    //     e.Run();
    //     sleep(1);
    // }
    return 0;
}